<?php
jimport('joomla.filesystem.file');
class stwupload{
	function upload($image,$thumb=0,$with=400,$heigh=550){
		$temp =  date('Ymdhis').rand(0,100).'_';
		$temp.= strtolower(str_replace(' ','_',JFile::makeSafe($image['name'])));
		$destImage=JPATH_SITE.DS.'uploads'.DS.'products'.DS.'norms'.DS.$temp;
		JFile::upload($image['tmp_name'],$destImage);
		if($thumb){
			$destThumbs=JPATH_SITE.DS.'uploads'.DS.'products'.DS.'thumbs'.DS.$temp;
			$tn = new Thumbnail($with,$heigh);
			$tn->loadFile($destImage);
			$tn->buildThumb($destThumbs);
		}		
		return $temp;
	}
	
	function prepareUpload($image){
		/*
		 * @$file is file you upload
		 */
		$filename = JFile::makeSafe($image['name']);
		switch(strtolower(JFile::getExt($filename) )){
			case 'jpg':	case 'png':case 'gif':case 'bmp':case 'jpeg':case 'pdf':
				return true;
			default:
				return false;		
		}
		
	}
	function deleteImage($image){
		$destImage=JPATH_SITE.DS.'uploads'.DS.'products'.DS.'norms'.DS.$image;
		if(JFile::exists($destImage))
			JFile::delete($destImage);
			
		$destThumb=JPATH_SITE.DS.'uploads'.DS.'products'.DS.'thumbs'.DS.$image;
		if(JFile::exists($destThumb))
			JFile::delete($destThumb);
		
	}

}
?>